package com.lul15834.orderfood.activity.user;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.lul15834.orderfood.MainActivity;
import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.user.dialog.UserBottomDialog;
import com.lul15834.orderfood.activity.user.fragment.UserBusinessCommentFragment;
import com.lul15834.orderfood.activity.user.fragment.UserBuyFragment;
import com.lul15834.orderfood.bean.UserBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//用户购买界面
public class UserBuyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_user_buy);
        //实现返回功能
        Toolbar toolbar= findViewById(R.id.user_buy_back);
        setSupportActionBar(toolbar);
        //返回两种方式，采用跳转和管理
        toolbar.setNavigationOnClickListener(v -> onBackPressed());
        Intent intent=getIntent();
        UserBean business= (UserBean) intent.getSerializableExtra("business");
        ImageView busImg= findViewById(R.id.user_buy_business_img);
        busImg.setImageBitmap(BitmapFactory.decodeFile(business.getsImg()));
        TextView name= findViewById(R.id.user_buy_business_name);
        name.setText(business.getsName());
        TextView des= findViewById(R.id.user_buy_business_des);
        des.setText("简介:"+business.getsDescribe());
        //实现显示数量
        TextView price=findViewById(R.id.user_buy_business_price);
        price.setText("0.00");
        TextView food=findViewById(R.id.user_buy_business_food);
        //点击结算按钮之后的事情
        Button btn=findViewById(R.id.user_buy_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String foodJson=food.getText().toString();
                JSONArray jsonArray=JSONArray.parseArray(foodJson);
                List<JSONObject> buyFoodList=new ArrayList<>();//存放所有购买商品的表
                for (Object o : jsonArray) {
                    JSONObject temp=JSONObject.parseObject(o.toString());
                    if(!temp.get("num").equals("0")){
                        buyFoodList.add(temp);
                    }
                }
                if (foodJson.isEmpty()){
                    Toast.makeText(UserBuyActivity.this,"未选择商品无法结算",Toast.LENGTH_SHORT).show();
                }else if (buyFoodList.size()==0){
                    Toast.makeText(UserBuyActivity.this,"未选择商品无法结算",Toast.LENGTH_SHORT).show();
                }else {
                    UserBottomDialog userBottomDialog=new UserBottomDialog(UserBuyActivity.this,business.getsId());
                }
            }
        });
        TabLayout table= findViewById(R.id.user_buy_table);
        ViewPager2 viewPager2=findViewById(R.id.user_buy_pager);
        viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                if (position==0){
                    return new UserBuyFragment(business.getsId(),UserBuyActivity.this);
                }else {
                    return new UserBusinessCommentFragment(business.getsId());
                }

            }

            @Override
            public int getItemCount() {
                return 2;
            }
        });
        new TabLayoutMediator(table,viewPager2,(tab, position) ->{
            if (position==0){
                tab.setText("点餐");
            }else {
                tab.setText("评论");
            }
        }).attach();

    }
}